package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public void add(SysDictType sysDictType) {
        sysDictTypeMapper.insert(sysDictType);
    }

    @Override
    public void deleteBatch(SysDictType sysDictType) {
        sysDictTypeMapper.deleteByPrimaryKey(sysDictType);
    }

    @Override
    public void edit(SysDictType sysDictType) {
        sysDictTypeMapper.updateByPrimaryKeySelective(sysDictType);
    }


    @Override
    public String getDictTypeUid(String dictType) {

        String s=sysDictTypeMapper.selectUidByDictType(dictType);

        return s;
    }

    @Override
    public List<String> getDictTypeUidList(List<String> dictTypeList) {
        List<String> list= new ArrayList<>();
        try {
            for (String s : dictTypeList) {
                String s1 = sysDictTypeMapper.selectUidByDictType(s);
                list.add(s1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Page<SysDictType> getList(Map<String, Object> searchMap) {

        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = createExample(searchMap);
        return (Page<SysDictType>) sysDictTypeMapper.selectByExample(example);

    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
                criteria.andLike("dictName", "%" + searchMap.get("keyword") + "%");
            }
            if (searchMap.get("dictName") != null && !"".equals(searchMap.get("dictName"))) {
                criteria.andEqualTo("dictName", searchMap.get("dictName"));//'字典名称'
            }
            if (searchMap.get("dictType") != null && !"".equals(searchMap.get("dictType"))) {
                criteria.andEqualTo("dictType", searchMap.get("dictType"));
            }
            if (searchMap.get("createByUid") != null && !"".equals(searchMap.get("createByUid"))) {
                criteria.andEqualTo("createByUid", searchMap.get("createByUid"));
            }
            if (searchMap.get("updateByUid") != null && !"".equals(searchMap.get("updateByUid"))) {
                criteria.andEqualTo("updateByUid", searchMap.get("updateByUid"));
            }
            if (searchMap.get("remark") != null && !"".equals(searchMap.get("remark"))) {
                criteria.andEqualTo("remark", searchMap.get("remark"));
            }
            if (searchMap.get("createTime") != null && !"".equals(searchMap.get("createTime"))) {
                criteria.andEqualTo("createTime", searchMap.get("createTime"));
            }
            if (searchMap.get("updateTime") != null && !"".equals(searchMap.get("updateTime"))) {
                criteria.andEqualTo("updateTime", searchMap.get("updateTime"));
            }
            if (searchMap.get("oid") != null && !"".equals(searchMap.get("oid"))) {
                criteria.andEqualTo("oid", searchMap.get("oid"));
            }
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andEqualTo("uid", searchMap.get("uid"));
            }
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("sort") != null && !"".equals(searchMap.get("sort"))) {
                criteria.andEqualTo("sort", searchMap.get("sort"));
            }
            if (searchMap.get("isPublish") != null && !"".equals(searchMap.get("isPublish"))) {
                criteria.andEqualTo("isPublish", searchMap.get("isPublish"));
            }
        }
        return example;
    }


}
